package com.briup.smartlabs.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.bean.SmartDeviceType;
import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.mapper.SmartDeviceTypeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "设备分类管理服务")
@RestController
public class SmartDeviceTypeController {
	@Autowired
	private SmartDeviceTypeMapper smartDeviceTypeMapper;
	private String typeName;
	private String id;
	
	@ApiOperation(value="分页显示设备分类信息")
	public Response<PageInfo<SmartDeviceType>> findByPage(
		@RequestParam(defaultValue="5")int pageSize,
		@RequestParam(defaultValue="1")int pageNum){
		PageHelper.startPage(pageNum, pageSize, true);
		List<SmartDeviceType> types=smartDeviceTypeMapper.selectAll();
		PageInfo<SmartDeviceType>pages=new PageInfo<>(types);
		return Response.ok(pages);
	
	}
	
	


	@ApiOperation(value="返回所有设备分类信息")
	@GetMapping("/deviceType/all")
	public Response<List<SmartDeviceType>> findAll(){
		List<SmartDeviceType>types=smartDeviceTypeMapper.selectAll();
		return Response.ok(types);
	}


	
	@DeleteMapping("/smartDeviceType/by/id")
	public Response<String>deleteById(String id){
		
		if(id==null) {
			return Response.error("请输入要删除的设备id");
			
		}
		smartDeviceTypeMapper.deleteByPrimaryKey(id);
		return Response.ok("删除成功");
	}
	
	
	@ApiOperation(value="保存或者更新设备信息",
			notes="1.如果设备信息不包含设备id(id),保存！<br/>"
			+"2.如果参数包含用户id(id),更新！")
	@PostMapping("/SmartDeviceType")
	public Response<String> saveOrUpdate(SmartDeviceType smartDeviceType){
		if(smartDeviceType.getId()==null) {
			//默认，如果数据没有传递主键值，就代表该数据在数据库中不存在，执行保存操作。
			smartDeviceType.setId(id);
			smartDeviceType.setTypeName(typeName);
		smartDeviceTypeMapper.insert(smartDeviceType);
		}else {
			smartDeviceTypeMapper.updateByPrimaryKey(smartDeviceType);
		}
		return Response.ok("操作成功！");
	}
	
	@ApiOperation(value="按照名字分页显示设备信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="name",value="设备名"),
		@ApiImplicitParam(name="pageNum",value="当前页码"),
		@ApiImplicitParam(name="pageSize",value="每页显示的个数"),
	})
	
	
	@GetMapping("/smartDeviceType/bypeName")
	public Response<PageInfo<SmartDeviceType>> findByTypeName(
			String typeName,
			@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "5")int pageSize){
		PageHelper.startPage(pageNum, pageSize,true);
		if(typeName!=null&&!"".equals(typeName)){
			typeName="%"+typeName+"%";
		}
		List<SmartDeviceType> types=smartDeviceTypeMapper.selestNameLike(typeName);
		PageInfo<SmartDeviceType> pages=new PageInfo<>(types);
		return Response.ok(pages);
	}




	@ApiOperation(value="按照设备id批量删除设备信息！")
	@DeleteMapping("smartDeviceType/by/batch")
	public Response<String> deleteByBath(String[] ids){
		if(ids==null||ids.length<=0) {
			return Response.error("请至少选择一个要删除的用户设备信息！");
		}
		
		for(String id:ids) {
			smartDeviceTypeMapper.deleteByPrimaryKey(id);
		}
		
		return Response.ok("删除成功！");
		

	}

}